/*
 * this is a test
 * I hope you can see this
 */


import javax.swing.*;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

import java.sql.ResultSet;
import java.sql.Statement;

@SuppressWarnings("serial")
public class Account extends JFrame {

	private JButton jbtCreate = new JButton("Create"); // Creating the Buttons
	private JButton jbtCancel = new JButton("Cancel");
	private JTextField FirstName;
	private JTextField LastName;
	private JTextField SS;
	private JTextField Phone; // Creating textfileds
	private JTextField Adress;
	private JTextField City;
	private JTextField ZipCode;
	private JTextField UserName;
	private JTextField Password;
	private JTextField Email;

	public static void main(String args[]) {

		Account frame = new Account(); // calling the class Account
		frame.setTitle("Pizza Account");

		frame.setSize(500, 450);
		frame.setLocationRelativeTo(null); // This is the frame
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);

	}

	@SuppressWarnings("unused")
	public Account() {
		CreateListener create = new CreateListener(); // calling the listener
														// method
		CreateListener cancel = new CreateListener();
		JPanel panel = new JPanel();
		setLayout(new GridLayout(11, 11, 1, 1));
		add(new JLabel("First Name:(A-Z or a-z)")); // Fields created to fill in
													// the account info
		FirstName = new JTextField(15);
		add(FirstName);

		add(new JLabel("Last Name:"));
		LastName = new JTextField(15);
		add(LastName);
		add(new JLabel("Email"));
		Email = new JTextField(25);
		add(Email);
		add(new JLabel("Social Security:"));
		SS = new JTextField(8);
		add(SS);
		add(new JLabel("Zipcode:"));
		ZipCode = new JTextField(5);
		add(ZipCode);
		add(new JLabel("Address:"));
		Adress = new JTextField(15);
		add(Adress);
		add(new JLabel("Phone Number:"));
		Phone = new JTextField(13);
		add(Phone);
		add(new JLabel("City:"));
		City = new JTextField(10);
		add(City);

		add(new JLabel("User Name:"));
		UserName = new JTextField(10);
		add(UserName);
		add(new JLabel("PassWord"));
		Password = new JTextField(10);
		add(Password);

		panel.add(jbtCreate); // adding the buttons to the panel
		panel.add(jbtCancel);
		jbtCreate.addActionListener(create); // adding the buttons to the
												// actionlistener method
		jbtCancel.addActionListener(create);
		this.add(panel, BorderLayout.NORTH);

	}

	private class CreateListener implements ActionListener { // Class for the
																// actionlistener

		@SuppressWarnings("unused")
		public void actionPerformed(ActionEvent e) { // The actionlistener
														// method
			if (e.getSource() == jbtCreate) { // if the create button is created
				

				UserMethods Method = new UserMethods();
				// After creating shutdown the window

				try {
					addUser(FirstName, LastName, Email, SS, ZipCode, Adress, Phone,
							City, UserName, Password);

					// Checking every content in the addUser method

				} catch (Exception e1) {

					e1.printStackTrace();
				}

			}

			else if (e.getSource() == jbtCancel) { // else Cancel button pressed
													// shutdown the window
				setVisible(false);
				System.exit(0);

			}

		}

	}

	@SuppressWarnings({ "unused", "static-access" })
	public void addUser(JTextField FirstName, JTextField LastName, JTextField Email,
			JTextField SS, JTextField ZipCode, JTextField Adress,
			JTextField Phone, JTextField City, JTextField UserName,
			JTextField Password) throws Exception {
	
		// The content checking method
		String firstname = FirstName.getText().toString(); // converting the
															// textfields into
															// strings
		String lastname = LastName.getText().toString(); // to check
		String email = Email.getText().toString();
		String ss = SS.getText().toString();
		String zipcode = ZipCode.getText().toString();
		String adress = Adress.getText().toString();
		String phone = Phone.getText().toString();
		String city = City.getText().toString();
		String username = UserName.getText().toString();
		String password = Password.getText().toString();
		UserMethods methods = new UserMethods();
		String error = "0";
		// Checking the inputs
		// First Name
		if (UserMethods.isFirstNameValid(firstname) == true) {
			firstname = UserMethods.oneCapitalPerWord(firstname);
			firstname = UserMethods.aphostropheToDash(firstname);

			System.out.println("          FirstName: " + firstname);
			FirstName.setBackground(Color.WHITE);
		} else{ 
			FirstName.setBackground(Color.PINK);
			error += "1";
		}
		// Last Name
		if (UserMethods.isLastNameValid(lastname) == true) {
			lastname = UserMethods.oneCapitalPerWord(lastname);
			lastname = UserMethods.aphostropheToDash(lastname);
			System.out.println("  		LastName: " + lastname);
			LastName.setBackground(Color.WHITE);
		} else{
			LastName.setBackground(Color.PINK);
			error += "1";
		}
		// Email
		if (UserMethods.mail(email) == true) {
			
			
			System.out.println("   		Email: " + email);
			Email.setBackground(Color.WHITE);
		} else {
			Email.setBackground(Color.PINK);
			error += "1";
		}
		
		// Personal Number
		if (UserMethods.iSocialSecurityValid(ss) == true) {
			// ss = removeNonLetters(ss);
			System.out.println("PersonNr: " + ss);
			SS.setBackground(Color.WHITE);
		} else{
			SS.setBackground(Color.PINK);
			error += "1";
		}
		// zipcode
		if (UserMethods.isZipCodeValid(zipcode) == true) {
			zipcode = UserMethods.removeNonLetters(zipcode);
			System.out.println("            zipcode: " + zipcode);
			ZipCode.setBackground(Color.WHITE);
		} else{
			ZipCode.setBackground(Color.PINK);
			error += "1";
		}
		// address
		if (UserMethods.isAddressValid(adress) == true) {
			UserMethods.oneCapitalPerWord(adress);
			System.out.println("            address: " + adress);
			Adress.setBackground(Color.WHITE);
		} else{
			Adress.setBackground(Color.PINK);
			error += "1";
		}
		// phonenumber
		if (UserMethods.checkAndReturnPhoneNumber(phone) != null) {
			phone = UserMethods.checkAndReturnPhoneNumber(phone);
			System.out.println("        phonenumber: " + phone);
			Phone.setBackground(Color.WHITE);
		} else{
			Phone.setBackground(Color.PINK);
			error += "1";
		}
		// City
		if (UserMethods.isCityValid(city) == true) {
			UserMethods.oneCapitalPerWord(city);
			System.out.println("               city: " + city);
			City.setBackground(Color.WHITE);
		} else{
			City.setBackground(Color.PINK);
			error += "1";
		}
		// username
		if (UserMethods.isUsernameValid(username) == true) {
			System.out.println("           username: " + username);
			UserName.setBackground(Color.WHITE);
		} else{
			UserName.setBackground(Color.PINK);
			error += "1";
		}
		// password
		if (UserMethods.isPasswordValid(password)) {
			System.out.println("           password: " + password);
			Password.setBackground(Color.WHITE);
		} else{
			Password.setBackground(Color.PINK);
			error += "1";
		}
		// Save to data base
		if (!error.contains("1")) {
			

			String C = "C";
			String UserQuery = "INSERT INTO User (`Group`,  `Username`, `Password`)"
					+ "VALUES ('"
					+ C
					+ "','"
					+ username
					+ "','"
					+ password
					+ "')";
			
			
						
			Login.db.InsertToDB(UserQuery);
			
			Statement st2 = Login.conn.createStatement();
			ResultSet rs = st2
					.executeQuery("select * from User where Username='"
							+ username + "' and Password='" + password + "'");
			String uname = "", pass = "";
			String ID = "", Group = "";
			if (rs.next()) {
				ID = rs.getString("U_ID");
				System.out.println(ID);
			}
			String query = "INSERT INTO Customer (`U_ID`, `Name`, `LastName`, `Email`, `PersonNumber`, `Zipcode`, `Address`, `PhoneNumber`, `City`)"
					+ "VALUES ('"
					+ ID
					+ "','"
					+ firstname
					+ "','"
					+ lastname
					+ "','"
					+ email
					+ "','"
					+ ss
					+ "','"
					+ zipcode
					+ "','"
					+ adress
					+ "','"
					+ phone + "','" + city + "')";
			
			
			Login.db.InsertToDB(query);
			 

			setVisible(false);

		} else {
			System.out.println("Something was wrong with this entry"); // else
																		// one
																		// or
																		// more
																		// of
																		// the
																		// contents
																		// are
																		// wrong
		}

	

		}
	}